/*******************************************************************************
 *
 *      Filename: test_Image.cpp
 *   Description: Simple test for Image class
 *        Author: [kb]ypp
 *    Created at: October 14, 2005
 *
 ******************************************************************************/

#include "../Image.h"

#include <cstdlib>
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <GL/glut.h>

GLuint tex;

void init()
{
	glClearColor( 0.0, 0.0, 0.0, 0.0 );
	glShadeModel( GL_FLAT );

	glGenTextures( 1, &tex );

	Image img;
	img.load( "file.ext" );
	//img.load( ".jpg" );
	//img.load( ".png" );
	//img.load( "../../../art/textures/r75logo.tga" );
	img.load( "../../../art/textures/grey_raw.tga" );
	img.load( "../../../art/textures/grey_rle.tga" );
	img.load( "../../../art/textures/greyalpha_raw.tga" );
	img.load( "../../../art/textures/greyalpha_rle.tga" );
	img.load( "../../../art/textures/rgb_raw.tga" );
	img.load( "../../../art/textures/rgb_rle.tga" );
	img.load( "../../../art/textures/rgba_raw.tga" );
	img.load( "../../../art/textures/rgba_rle.tga" );
	//img.load( "../../../art/textures/test.ppm" );
	//img.save( "../../../art/textures/test2.ppm" );

	glEnable( GL_TEXTURE_2D );
	glBindTexture( GL_TEXTURE_2D, tex );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
	GLenum format;
	switch ( img.format() ) {
		case Image::eNone:
			cerr << "Image not loaded" << endl;
			exit( EXIT_FAILURE );
		case Image::eLuminance:
			format = GL_LUMINANCE;
			break;
		case Image::eLuminanceAlpha:
			format = GL_LUMINANCE_ALPHA;
			break;
		case Image::eRGB:
			format = GL_RGB;
			break;
		case Image::eRGBA:
			format = GL_RGBA;
			break;
	}
	glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
			img.width(), img.height(),
			0, format, GL_UNSIGNED_BYTE, img.data() );
	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
}

void display()
{
	glClear( GL_COLOR_BUFFER_BIT );

	glBegin( GL_QUADS );
		glTexCoord2f( 0.0, 0.0 );  glVertex2f( -5.0, -5.0 );
		glTexCoord2f( 1.0, 0.0 );  glVertex2f(  5.0, -5.0 );
		glTexCoord2f( 1.0, 1.0 );  glVertex2f(  5.0,  5.0 );
		glTexCoord2f( 0.0, 1.0 );  glVertex2f( -5.0,  5.0 );
	glEnd();

	glFlush();
}

void reshape( int w, int h )
{
	glViewport( 0, 0, static_cast< GLsizei >( w ), static_cast< GLsizei >( h ) );
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	glOrtho( -5.5, 5.5, -5.5, 5.5, -1.0, 1.0 );
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
}

void keyboard( unsigned char key, int x, int y )
{
	switch ( key ) {
		case 27 /*Esc*/:
			exit( 0 );
			break;
		default:
			break;
	}
}

int main( int argc, char *argv[] )
{
	glutInit( &argc, argv );
	glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
	glutInitWindowSize( 300, 300 );
	glutInitWindowPosition( 100, 100 );
	glutCreateWindow( "Image class test" );
	init();
	glutDisplayFunc( display );
	glutReshapeFunc( reshape );
	glutKeyboardFunc( keyboard );
	glutMainLoop();

	return 0;
}
